package cn.com.backstage.controller.base;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.com.api.entity.ApiShop;
import cn.com.api.entity.ApiT_city;
import cn.com.api.entity.ApiT_products;
import cn.com.api.entity.ApiType;
import cn.com.api.service.ApiShopsService;
import cn.com.api.service.ApiT_productsService;
import cn.com.api.service.ApiTypeService;
import cn.com.api.service.StartOnLoadService;
import cn.com.util.*;
import cn.com.util.*;
import cn.com.util.*;


public class T_productsBaseController {

	private static Logger logger = Logger.getLogger(T_productsBaseController.class);
	
	@Autowired
	private ApiT_productsService apiT_productsService;
	@Autowired
	private ApiTypeService apiTypeService;
	@Autowired
	private ApiShopsService apiShopsService;
	@Autowired
	private StartOnLoadService startOnLoadService;
	
	/**
	 * 自营货品列表
	 * @return
	 */
	@RequestMapping(value = "t_products-list")
	public ModelAndView t_products_list(HttpServletRequest request, HttpServletResponse response,PageInfo pageInfo) throws Exception{
		ModelAndView mv = new ModelAndView("forward:/backstage/t_products_list.jsp");
		Map<String,Object> conditions = new HashMap<String,Object>();
		String name = request.getParameter("name");
		String type = request.getParameter("type");
		if (type == null){
			type = "-1";
		}
		String type1 = request.getParameter("type1");
		String type2 = request.getParameter("type2");
		String type3 = request.getParameter("type3");
		String cityId = request.getParameter("city_id");
		String store = request.getParameter("store");
		conditions.put("name", name != null ? name.trim() : name );
		conditions.put("type", type != null ? type.trim() : type);
		conditions.put("type1", type1 != null ? type1.trim() : type1);
		conditions.put("type2", type2 != null ? type2.trim() : type2);
		conditions.put("type3", type3 != null ? type3.trim() : type3);
		conditions.put("store", store != null ? store.trim() : store);
		conditions.put("city_id",cityId);
		conditions.put("isZiYing", true);
		conditions.put("cEnable", true);
		
		List<Integer> endTypes = new ArrayList<Integer>();
	
		Integer parentTypeId  = Integer.parseInt(type);
		
		if (parentTypeId != -1){
			endTypes.add(parentTypeId);
		}
		
		//二级
		List<Integer> seconedTypes = apiTypeService.getZiYingSubTypes(parentTypeId);
		//三级
		for (Integer parentId : seconedTypes ){
			List<Integer> subs = apiTypeService.getZiYingSubTypes(parentId);
			endTypes.addAll(subs);
		}
		endTypes.addAll(seconedTypes);
		
		conditions.put("types", endTypes);
		
		List<ApiT_products> t_productss = apiT_productsService.searchProducts(conditions, pageInfo);
		
		
		List<ApiShop> shops = apiShopsService.searchShops(conditions);
		mv.addObject("shops", shops);

		List<ApiT_city> citys = startOnLoadService.citys();
		mv.addObject("citys", citys);

		List<ApiType> types = new ArrayList<ApiType>();
		List<ApiType> types1 = apiTypeService.getBigTypesZiYing();
		// 包含根级分类
		types.addAll(types1);
		List<ApiType> types2 = apiTypeService.getSecondTyes(true);
		types.addAll(types2);
		List<ApiType> types3 = apiTypeService.getThirdTypes(true);
		types.addAll(types3);
		mv.addObject("types", types);
		mv.addObject("types1", types1);
		mv.addObject("types2", types2);
		mv.addObject("types3", types3);
		mv.addObject("conditions", conditions);
		mv.addObject("t_productss", t_productss);
		mv.addObject("pageInfo", pageInfo);
		return mv;
	}
	
	/**
	 * 进入新增或者编辑页面
	 * @return
	 */
	@RequestMapping(value = "t_products-add-pre")
	public ModelAndView t_products_add_pre(HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView mv = new ModelAndView("forward:/backstage/t_products_add.jsp");
		//
		if(request.getParameter("id")!=null){
			String id = request.getParameter("id");
			ApiT_products t_products = apiT_productsService.findT_productsById(Long.valueOf(id));
			mv.addObject("t_products",t_products);
		}
		return mv;
	}

	/**
	 *新增或者编辑
	 * @return
	 */
	@RequestMapping(value = "t_products-add")
	public void t_products_add(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String product_id = request.getParameter("product_id");
		String goods_id = request.getParameter("goods_id");
		String sku_code = request.getParameter("sku_code");
		String erp_barcode = request.getParameter("erp_barcode");
		String erp_product_id = request.getParameter("erp_product_id");
		String erp_goods_sn = request.getParameter("erp_goods_sn");
		String mkt_price = request.getParameter("mkt_price");
		String price = request.getParameter("price");
		String spec = request.getParameter("spec");
		String pdt_spec = request.getParameter("pdt_spec");
		String store = request.getParameter("store");
		String unit = request.getParameter("unit");
		String is_delete = request.getParameter("is_delete");
		String city_id = request.getParameter("city_id");
		String shop_id = request.getParameter("shop_id");
		String create_time = request.getParameter("create_time");
		String update_time = request.getParameter("update_time");
		String description = request.getParameter("description");
		String status = request.getParameter("status");
		String sold = request.getParameter("sold");
		String is_top = request.getParameter("is_top");
		
		Map<String,Object> map = new HashMap<String,Object>();
		String id = request.getParameter("id");
		//add
		if(id==null || (id.trim()).equals("")){
			ApiT_products t_products = new ApiT_products();
          
		    t_products.setGoods_id(Long.valueOf(goods_id));
		    t_products.setSku_code(sku_code);
		    t_products.setErp_barcode(erp_barcode);
		    t_products.setErp_product_id(erp_product_id);
		    t_products.setErp_goods_sn(erp_goods_sn);
		    t_products.setMkt_price(Util.getBigDecimal(mkt_price));
		    t_products.setPrice(Util.getBigDecimal(price));
		    t_products.setSpec(spec);
		    t_products.setPdt_spec(pdt_spec);
		    t_products.setStore(Integer.valueOf(store));
		    t_products.setUnit(unit);
		    //t_products.setIs_delete(Integer.valueOf(is_delete));
		    t_products.setCity_id(Integer.valueOf(city_id));
		    t_products.setShop_id(Integer.valueOf(shop_id));
		    t_products.setCreate_time(create_time);
		    t_products.setUpdate_time(update_time);
		    t_products.setDescription(description);
		    //t_products.setStatus(Integer.valueOf(status));
		    //t_products.setSold(Integer.valueOf(sold));
		    //t_products.setIs_top(Integer.valueOf(is_top));
	      
			apiT_productsService.insertT_products(t_products);
		}
		//edit
		else{
			ApiT_products t_products = apiT_productsService.findT_productsById(Long.valueOf(id));
	      
		    t_products.setGoods_id(Long.valueOf(goods_id));
		    t_products.setSku_code(sku_code);
		    t_products.setErp_barcode(erp_barcode);
		    t_products.setErp_product_id(erp_product_id);
		    t_products.setErp_goods_sn(erp_goods_sn);
		    t_products.setMkt_price(Util.getBigDecimal(mkt_price));
		    t_products.setPrice(Util.getBigDecimal(price));
		    t_products.setSpec(spec);
		    t_products.setPdt_spec(pdt_spec);
		    t_products.setStore(Integer.valueOf(store));
		    t_products.setUnit(unit);
		    //t_products.setIs_delete(Integer.valueOf(is_delete));
		    t_products.setCity_id(Integer.valueOf(city_id));
		    t_products.setShop_id(Integer.valueOf(shop_id));
		    t_products.setCreate_time(create_time);
		    t_products.setUpdate_time(update_time);
		    t_products.setDescription(description);
		    //t_products.setStatus(Integer.valueOf(status));
		    //t_products.setSold(Integer.valueOf(sold));
		    //t_products.setIs_top(Integer.valueOf(is_top));
		
			apiT_productsService.updateT_products(t_products);
		}
		//
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "添加成功！");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}
	
	/**
	 *删除
	 * @return
	 */
	@RequestMapping(value = "t_products-delete")
	public void t_products_delete(HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		Map<String,Object> map = new HashMap<String,Object>();
		String id = request.getParameter("id");
		
		//delete
		int ret_i =	apiT_productsService.deleteT_products(Integer.valueOf(id));
		
		//
		if(ret_i > 0){
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "删除成功！");
		}
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}
	
}



